﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using MTGScore_Helps.Resources;
using Microsoft.Phone.Tasks;
using System.Threading;

namespace MTGScore_Helps
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor

        public static string MTGScore_ID = "5b8e1d81-a866-e011-81d2-78e7d1fa76f8";
        public static string MTGCards_ID = "8e788bb0-9067-e011-81d2-78e7d1fa76f8";
        public static string MTGHelps_ID = "8ee0b8fe-8e67-e011-81d2-78e7d1fa76f8";
        public static string MTGDice_ID = "db4da84e-5768-e011-81d2-78e7d1fa76f8";
        public static string InformaTreni_ID = "75c4de83-134c-e011-854c-00237de2db9e";
        public static string ChainingExplosion_ID = "6f6e33a9-6558-e011-854c-00237de2db9e";
        public static string EbookReader_ID = "c7ded5b0-17d6-df11-a844-00237de2db9e";
        public static string TapSequence_ID = "3cf38972-3573-e011-81d2-78e7d1fa76f8";
        public static string BoardGameSuite_ID = "e8201966-ff70-e011-81d2-78e7d1fa76f8";
        public static string Enigma_ID = "ad6556b4-bf71-e011-81d2-78e7d1fa76f8";
        public static string IdeoPuzzle_ID = "f7ffd022-63dc-df11-a844-00237de2db9e";
        public static string TriangleShooter_ID = "a08a8336-3cfe-df11-9264-00237de2db9e";
        public static string SapceBattle_ID = "0ee14cb1-cad9-df11-a844-00237de2db9e";

        List<Set> helps;// = new List<Set>();

        public MainPage()
        {
            InitializeComponent();
            
            DataContext = App.ViewModel;
            Ads_List.ItemsSource = App.ViewModel.Ads;
            Microsoft.Advertising.Mobile.UI.AdControl.TestMode = false;
            ApplicationBar.Buttons.Clear();
            ApplicationBarIconButton credits = new ApplicationBarIconButton() { Text = Strings.Credits, IsEnabled = true };

            if (App.CurrentTheme == App.Theme.Dark)
                credits.IconUri = new Uri("\\Images\\dark\\appbar.questionmark.rest.png", UriKind.Relative);
            else
                credits.IconUri = new Uri("\\Images\\light\\appbar.questionmark.rest.png", UriKind.Relative);
            credits.Click += OpenCredits_Button_Click;
            ApplicationBar.Buttons.Add(credits);
            //if (!App.isTrial())
            //{
            //    //AdControlGrid.Visibility = System.Windows.Visibility.Collapsed;
            //    //AdControl.Visibility = System.Windows.Visibility.Collapsed;

            //    Ads1_Button.Visibility = System.Windows.Visibility.Collapsed;
            //    Grid.SetRowSpan(Results_List, 2);
            //}
            helps = Helps.GetHelps();

            if (Helps_List.Items.Count == 0)
            {
                helps = Helps.GetHelps();
                for (int i = 0; i < helps.Count; i++)
                {
                    var h = helps[i];
                    Helps_List.Items.Add(new ListBoxItem() { Content = h.name, FontSize = 30 });
                }
            }
            try
            {
                var occ = Research.LoadUtilizations() + 1;
                new Thread(new ThreadStart(() =>
                {
                    Research.getHTML(Research.GetUtilizationUrl(occ, App.isTrial()),
                        (s) =>
                        {
                            Research.DeleteUtilizations();
                        },
                        () =>
                        {
                            Research.SaveUtilizations(occ);
                        });
                })).Start();
            }
            catch { }
        }
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }
        private void OpenCredits_Button_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/CreditsPage.xaml", UriKind.Relative));
        }

        
        public static bool showProducts = false;
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (showProducts)
            {
                showProducts = false;
                Ads1_Button_Click(null, null);
            }
        }
        

        private void Ads_List_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Ads_List.SelectedItem == null)
                return;
            AdsViewModel ads = Ads_List.SelectedItem as AdsViewModel;
            Ads_List.SelectedItem = null;
            if (ads.Name != Strings.OtherProducts && ads.ID != "")
                OpenMarket(ads.ID);

            
        }


        private void Ads1_Button_Click(object sender, RoutedEventArgs e)
        {
            PivotControl.SelectedItem = AdsPivot_Name;
        }

        private void OpenMarket(string ID)
        {

            MarketplaceDetailTask marketplaceDetailTask = new MarketplaceDetailTask();
            marketplaceDetailTask.ContentIdentifier = ID;
            marketplaceDetailTask.Show();
        }

        private void Helps_List_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Helps_List.SelectedItem == null)
                return;
            try
            {
                ListBoxItem item = (ListBoxItem)Helps_List.SelectedItem;

                for (int i = 0; i < helps.Count; i++)
                    if (item.Content.Equals(helps[i].name))
                    {
                        var h = helps[i];
                        if (h.helps.Count == 0)
                            return;
                        PhoneApplicationService.Current.State.Remove("SelectedHelp");
                        PhoneApplicationService.Current.State.Add("SelectedHelp", h);
                        NavigationService.Navigate(new Uri("/HelpPage.xaml", UriKind.Relative));
                        break;
                    }

            }
            catch (Exception ex)
            {
                ex.ToString();
            }
            finally
            {
                Helps_List.SelectedItem = null;
            }
        }

        private void AdControl_AdControlError(object sender, Microsoft.Advertising.Mobile.UI.ErrorEventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                Ads1_Button.Visibility = System.Windows.Visibility.Visible;
                AdControl.Visibility = System.Windows.Visibility.Collapsed;
            });
        }
    }
}